/**
 * 
 */
package jp.co.withone.osgi.gadget.bundlecontroller.viewer.impl;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import jp.co.withone.osgi.gadget.bundlecontroller.manager.ViewManager;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;

/**
 * @author takada
 *
 */
public class TrayIconMouseAdapter extends MouseAdapter  {

    private static final String MENU_ITEM_LABEL_EXIT = "popup.message.exit";
    private static final String MENU_ICON_EXIT_LOCATION = "/jp/co/withone/osgi/gadget/bundlecontroller/resource/menu_exit_icon.png";
    private static final String MENU_ICON_ACTIVE_LOCATION = "/jp/co/withone/osgi/gadget/bundlecontroller/resource/menu_active_icon.png";
    private static final String MENU_ICON_INACTIVE_LOCATION = "/jp/co/withone/osgi/gadget/bundlecontroller/resource/menu_inactive_icon.png";
    private ViewManager manager = null;
    
    public TrayIconMouseAdapter(ViewManager manager) {
        this.manager = manager;
        
    }
    
    /* (no Javadoc)
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
     */
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
            JPopupMenu jPopupMenu = getBundleMenuItems();
            jPopupMenu.setLocation(e.getX(), e.getY());
            jPopupMenu.setInvoker(jPopupMenu);
            jPopupMenu.setVisible(true);

        }
    }
    
    /* (no Javadoc)
     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
            JPopupMenu jPopupMenu = getBundleMenuItems();
            jPopupMenu.setLocation(e.getX(), e.getY());
            jPopupMenu.setInvoker(jPopupMenu);
            jPopupMenu.setVisible(true);

        }
    }
    
    private JPopupMenu getBundleMenuItems(){
        Bundle[] bundles = manager.getBundleContext().getBundles();
        if (bundles == null || bundles.length == 0){
            return new JPopupMenu();
        }
        JPopupMenu popupMenu = new JPopupMenu();
        for (Bundle bundle : bundles) {
            if (bundle.getBundleId() == 0|| bundle.equals(manager.getBundleContext().getBundle())){
                continue;
            }
            popupMenu.add(getBundleMenuItem(bundle));
        }
        // add Install menu
        popupMenu.addSeparator();
        popupMenu.add(getControlMenuItem(BundleActionCommand.INSTALL, null));
        // add Refresh menu
        popupMenu.addSeparator();
        popupMenu.add(getControlMenuItem(BundleActionCommand.REFRESH, null));
        // add Exit
        popupMenu.addSeparator();
        popupMenu.add(getExitMenuItem());
        return popupMenu;
    }
    
    private JMenu getBundleMenuItem(Bundle bundle){
        String bundleName = (String)bundle.getHeaders().get(Constants.BUNDLE_NAME);
        JMenu menu = new JMenu(bundleName);
        if (bundle.getState() == Bundle.ACTIVE){
            menu.setIcon(manager.getImageIcon(MENU_ICON_ACTIVE_LOCATION));
        } else {
            menu.setIcon(manager.getImageIcon(MENU_ICON_INACTIVE_LOCATION));
            menu.setForeground(new Color(150,150,150));
        }
        if (manager.getSettingViewListener(bundle) != null){
            menu.add(getControlMenuItem(BundleActionCommand.SETTING, bundle));
            menu.addSeparator();
        }
        menu.add(getControlMenuItem(BundleActionCommand.START, bundle));
        menu.add(getControlMenuItem(BundleActionCommand.STOP, bundle));
        menu.add(getControlMenuItem(BundleActionCommand.UNINSTALL, bundle));
        menu.add(getControlMenuItem(BundleActionCommand.REFRESH, bundle));
        return menu;
    }

    private JMenuItem getControlMenuItem(BundleActionCommand actionCommand ,Bundle bundle) {
        JMenuItem menuItem = new JMenuItem(manager.getMessage(actionCommand.getPopupLabel()),manager.getImageIcon(actionCommand));
        menuItem.addMouseListener(new BundleControlMouseAdapter(manager,bundle,actionCommand));
        menuItem.setActionCommand(actionCommand.toString());
        return  menuItem;
    }
    
    private JMenuItem getExitMenuItem() {
        JMenuItem menuItem = new JMenuItem(manager.getMessage(MENU_ITEM_LABEL_EXIT),manager.getImageIcon(MENU_ICON_EXIT_LOCATION));
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               Bundle bundle =  manager.getBundleContext().getBundle(0);
               if (bundle != null) {
               try {
                    bundle.stop();
                } catch (BundleException e1) {
                    e1.printStackTrace();
                    System.exit(1);
                }
               }
                   System.exit(0);
            }
        });
        return menuItem;
    }
    
}
